package cn.iocoder.yudao.module.call.service.vosnumber;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.call.controller.admin.location.vo.LocationPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.location.vo.LocationSaveReqVO;
import cn.iocoder.yudao.module.call.controller.admin.vosnumber.vo.VosNumberPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.vosnumber.vo.VosNumberSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.location.LocationDO;
import cn.iocoder.yudao.module.call.dal.dataobject.vosnumber.VosNumberDO;
import cn.iocoder.yudao.module.call.dal.mysql.channel.ChannelMapper;
import cn.iocoder.yudao.module.call.dal.mysql.location.LocationMapper;
import cn.iocoder.yudao.module.call.dal.mysql.vosnumber.VosNumberMapper;
import cn.iocoder.yudao.module.call.service.location.LocationService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.call.enums.ErrorCodeConstants.NUMBER_POOL_NOT_EXISTS;

/**
 * 号码池 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class VosNumberServiceImpl implements VosNumberService {

    @Resource
    private VosNumberMapper vosNumberMapper;

    @Override
    public Long createVosNumber(VosNumberSaveReqVO createReqVO) {
        // 插入
        VosNumberDO numberPool = BeanUtils.toBean(createReqVO, VosNumberDO.class);
        vosNumberMapper.insert(numberPool);
        // 返回
        return numberPool.getId();
    }

    @Override
    public void updateVosNumber(VosNumberSaveReqVO updateReqVO) {
        // 更新
        VosNumberDO updateObj = BeanUtils.toBean(updateReqVO, VosNumberDO.class);
        vosNumberMapper.updateById(updateObj);
    }

    @Override
    public void deleteVosNumber(Long id) {
        vosNumberMapper.deleteById(id);
    }

    @Override
    public VosNumberDO getVosNumber(Long id) {
        return vosNumberMapper.selectById(id);
    }

    @Override
    public PageResult<VosNumberDO> getVosNumberPage(VosNumberPageReqVO pageReqVO) {
        return vosNumberMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getNumberCount(VosNumberPageReqVO pageReqVO) {
        return vosNumberMapper.selectCount(pageReqVO);
    }

    @Override
    public Map<Long, Long> getNumberCount(Collection<Long> ids) {
        Map<Long, Long> result = new HashMap<>();
        ids.forEach(id -> {
            result.put(id, vosNumberMapper.selectCount(VosNumberDO::getLocationId, id));
        });
        return result;
    }

    @Override
    public List<Map<String, Long>> getVosNumberCount() {
        return vosNumberMapper.getVosNumberCount();
    }
}